package com.core.service;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import com.core.dao.DAOException;
import com.core.dao.DAOFactory;
import com.core.dao.impl.WorkDAOImpl;
import com.core.entities.Contact;
import com.core.entities.Work;

public class WorkSrv {
	
	private Connection conn;
	private WorkDAOImpl workDAOImpl;

	public WorkSrv(Connection conn) {

		this.conn = conn;
	}
	
	public List<String> getWorksNames(){
		
		List<String> worksNames = new ArrayList<String>();
		try{
			for(Work work : DAOFactory.getInstance().getWorkDAO(conn).selectAll())
				worksNames.add(work.getCompnyName());
		}catch(DAOException e){
			e.printStackTrace();
		}
		return worksNames;
	}
	
	public void getWorksByCity(String cityName){
		List<String> worksNames = new ArrayList<String>();
		try{
			int cityId = DAOFactory.getInstance().getCityDAO(conn)
					.getByName(cityName).get(0).getId();
			for(Work work : DAOFactory.getInstance().getWorkDAO(conn).getByCityId(cityId))
				worksNames.add(work.getCompnyName());
		}catch(DAOException e){
			e.printStackTrace();
		}
	}
	public void getWorksByTwitterOnlineAccount(String twitterOnlineAccountName){
		List<String> woksNames = new ArrayList<String>();
		try{
			int onlineAccountId = DAOFactory.getInstance().getOnlineAccountDAO(conn)
					.getByTwitter(twitterOnlineAccountName).get(0).getId();
			for(Work work : DAOFactory.getInstance().getWorkDAO(conn).getByOnlineAccountId(onlineAccountId))
				woksNames.add(work.getCompnyName());
		}catch(DAOException e){
			e.printStackTrace();
		}
	}
	public void getWorksByGoogleOnlineAccount(String googleOnlineAccountName){
		List<String> worksNames = new ArrayList<String>();
		try{
			int onlineAccountId = DAOFactory.getInstance().getOnlineAccountDAO(conn)
					.getByGoogle(googleOnlineAccountName).get(0).getId();
			for(Work work : DAOFactory.getInstance().getWorkDAO(conn).getByOnlineAccountId(onlineAccountId))
				worksNames.add(work.getCompnyName());
		}catch(DAOException e){
			e.printStackTrace();
		}
	}
	public void getWorksByFacebookOnlineAccount(String facebookOnlineAccountName){
		List<String> worksNames = new ArrayList<String>();
		try{
			int onlineAccountId = DAOFactory.getInstance().getOnlineAccountDAO(conn)
					.getByFacebook(facebookOnlineAccountName).get(0).getId();
			for(Work work : DAOFactory.getInstance().getWorkDAO(conn).getByOnlineAccountId(onlineAccountId))
				worksNames.add(work.getCompnyName());
		}catch(DAOException e){
			e.printStackTrace();
		}
	}
	
	public int getWorkPK(String compnyName){
		try{
			return DAOFactory.getInstance().getWorkDAO(conn).getByCompnyName(compnyName).get(0).getId();
		}catch(DAOException e){
			e.printStackTrace();
		}
		return -1;
	}
	public WorkDAOImpl getWorkDAO() throws DAOException{
		if(workDAOImpl == null)
			workDAOImpl = (WorkDAOImpl) DAOFactory.getInstance().getWorkDAO(conn);
		return workDAOImpl;
	}
	
	

}
